package cn.webrx;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileCountRows {
    public static void main(String[] args) {
        String file = "C:\\javase2203\\ch09\\src\\cn\\webrx\\Dir1.java";
        try (var f = new FileInputStream(file)) {
            String src = new String(f.readAllBytes());
            //方法一
            //System.out.println(src.lines().count());

            //方法二
            Pattern p = Pattern.compile("\n");
            Matcher matcher = p.matcher(src);
            int count = 0;
            while (matcher.find()) {
                ++count;
            }
            System.out.println(count);
        } catch (Exception e) {

        }

        BufferedInputStream bis;

        //方法三  读取一个java源码文件，分别添加行号，并保存输出到别一个文件中。
        try (
                var buf = new BufferedReader(new FileReader(file));
                FileOutputStream fos = new FileOutputStream("mysrc.txt");
        ) {
            int c = 0;
            String line = null;
            while ((line = buf.readLine()) != null) {
                String lin = String.format("%d、%s%n", ++c, line);
                fos.write(lin.getBytes());
            }
        } catch (Exception e) {

        }
    }
}
